<?php

/*
 *
 * BEGIN live_view_ui.php
 *
 *
 * Dazzler Full ScoreCard Uis
 * @Description: The class creates all the UIs for the live view in for the cricket fan side
 * @Authors: Salman, Wits University
 * @Copyright: © Wits University 2011 - All Rights Reserved
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 *
 *
 * BEGIN CHANGELOG
 * Developers working on thee files are REQUIRED to maintain this CHANGELOG.
 * Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
 * !==================================================================================================================================================================
 *
 * VERSION 1.0.0
 * 	+ File Created, Salman, 15 October 2011
 *
 * ==================================================================================================================================================================!
 * END CHANGELOG
 */


/* * *****************************************************************************************************************************************************************
 *
 * BEGIN Direct Call Check
 *
  /***************************************************************************************************************************************************************** */
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1) {
    /* Read the ini language file */
    $ini_lang_array = array();
    $clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
    $lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
    die($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/* * *****************************************************************************************************************************************************************
 *
 * END Direct Call Check
 *
  /***************************************************************************************************************************************************************** */

class live_view_ui extends baseClass {

    private $htmlelementsbuilder;
    private $gameID = null;
    private $innings = null;
    private $parsesiteconfig;
    private $StatsAdminObj;

    public function __construct($gameID, $innings) {
        $this->htmlelementsbuilder = new htmlelementsbuilder();
        $this->parsesiteconfig = new parsesiteconfig();
        parent::parseLanguageErrorLogFile();
        if (isset($gameID)) {
            $this->gameID = $gameID;
        }
        if (isset($innings)) {
            $this->innings = $innings;
        }
        $this->StatsAdminObj = new StatsAdmin();
    }

    private function checkNum($num) {
        return ($num % 2) ? TRUE : FALSE;
    }

    private function buildCanvas() {
        $canvascreator = new canvascreator(800, 800);
        return "<div id='wagonWheel' style='float:left; clear:left; margin:0px 0px 0px 10px;'>" . $canvascreator->getLiveMatchCanvas() . "</div>";
    }

    private function buildLiveViewDisplay() {
        $liveViewDisplay = NULL;
     $liveViewDisplay .= "<style>
ul {
	margin: 10px;
}
#ticker-wrapper{
width:95%;

}
ul#deliveryInfo {
-webkit-border-radius: 20px;
	-moz-border-radius: 20px;
	border-radius: 20px;
border: 1px solid #ccc;
	padding: 10px 10px 25px 10px;
	background: #F3F3F3;
}
.js-hidden {display:none;}
     </style>";
        $liveViewDisplay .= "<div id='liveViewDisplayContainer' style='float:left; clear:right; margin:0px 0px 0px 10px;'>";
        $StatsAdmin = new StatsAdmin();
        $lastBallInfoArray = $StatsAdmin->getLastBallInfo($this->gameID);
        $liveViewDisplay.= "<h3>Delivery Information</h3>";
        $liveViewDisplay.= "		<div id='ticker-wrapper'>
            <ul id='deliveryInfo'>";
         $liveViewDisplay.= "<li style='font-size:120%;'><b><sup>DELIVERY INFO</sup></b>&nbsp;&nbsp;&nbsp;&nbsp;".$lastBallInfoArray['BALLBYBALL_BOW_BALLTYP']."</li>";
         $liveViewDisplay.= "<li style='font-size:120%;'><b><sup>SHOT TYPE</sup></b>&nbsp;&nbsp;&nbsp;&nbsp;".$lastBallInfoArray['BALLBYBALL_BAT_SHOTTYP']."</li>";
         $liveViewDisplay.= "<li style='font-size:120%;'><b><sup>RUNS MADE</sup></b>&nbsp;&nbsp;&nbsp;&nbsp;".$lastBallInfoArray['BALLBYBALL_RUNS']."</li>";
         $liveViewDisplay.= "<li style='font-size:120%;'><b><sup>BALL TIME</sup></b>&nbsp;&nbsp;&nbsp;&nbsp;".$lastBallInfoArray['BALLBYBALL_TIME']."</li>";
                  if ($lastBallInfoArray['BALLBYBALL_EXTR'] == "1")
         {
           $liveViewDisplay.= "<li style='font-size:120%;'><b><sup>EXTRA INFO</sup></b>&nbsp;&nbsp;&nbsp;&nbsp;".$lastBallInfoArray['BALLBYBALL_EXTRATYPE']."</li>";
         }


$liveViewDisplay.= '</ul> </div>';
        $liveViewDisplay.= "<h3>Bowler Bowling</h3>";
        $liveViewDisplay.= '<table width="100%" cellpadding="3" cellspacing="0" style="font-size:95%;">
            <tr>
                    <th align="left" width="210">Bowler</th>
                    <th width="40">Runs</th>
                    <th width="40">Wickets</th>
                    <th width="40">Balls Bowled</th>
                    <th width="40">Overs</th>
                    <th width="40">Economy</th>
                    <th>Extras</th>

            </tr>';
        $liveViewDisplay .="<tr bgcolor='#F3F3F3'>";
                $liveViewDisplay .="<td >" . $lastBallInfoArray['BOWLER_FIRST_NAME'] . " " . $lastBallInfoArray['BOWLER_LAST_NAME'] . "*</td>
        <td align='center'>" . $lastBallInfoArray['BOWLER_RUNS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['BOWLER_WICKETS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['BOWLER_BALLS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['BOWLER_OVER'] . "</td>
        <td align='center'>" . $lastBallInfoArray['BOWLER_ECONOMY'] . "</td>";

          $extraTypesWithOutNulls = array_filter($lastBallInfoArray['BOWLER_EXTRATYPES'], 'strlen');
  $extraTypesCountArray = array_count_values($extraTypesWithOutNulls);
  $liveViewDisplay .="<td align='right'>";
  foreach ($extraTypesCountArray as $extraType =>$value)
  {

       $liveViewDisplay .= "(".$value." ".$extraType.")&nbsp; ";


  }
  $liveViewDisplay .="</td ><tr>";
        $liveViewDisplay .= "</table>";

        $liveViewDisplay.= "<h3>Batsmen Batting</h3>";


        $liveViewDisplay.= '<table width="100%" cellpadding="3" cellspacing="0" style="font-size:95%;">
            <tr>
                    <th align="left" width="210">Batsman</th>
                    <th width="65">Runs</th>
                    <th width="65">Balls Faced</th>
                    <th width="65">Strike Rate</th>
                    <th width="75">Number of Fours Scored</th>
                    <th >Number of Sixes Scored</th>

            </tr>';
        $liveViewDisplay .="<tr bgcolor='#F3F3F3'>";
        $liveViewDisplay .="<td >" . $lastBallInfoArray['FACING_BATSMAN_FIRST_NAME'] . " " . $lastBallInfoArray['FACING_BATSMAN_LAST_NAME'] . "*</td>
        <td align='center'>" . $lastBallInfoArray['FACING_BATSMAN_RUNS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['FACING_BATSMAN_BALLS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['FACING_BATSMAN_SR'] . "</td>
        <td align='center'>" . $lastBallInfoArray['FACING_BATSMAN_FOURS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['FACING_BATSMAN_SIXES'] . "</td>
        </tr>";
        $liveViewDisplay .= "<tr>";
        $liveViewDisplay .="<td>" . $lastBallInfoArray['UNFACING_BATSMAN_FIRST_NAME'] . " " . $lastBallInfoArray['UNFACING_BATSMAN_LAST_NAME'] . "</td>
        <td align='center'>" . $lastBallInfoArray['UNFACING_BATSMAN_RUNS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['UNFACING_BATSMAN_BALLS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['UNFACING_BATSMAN_SR'] . "</td>
        <td align='center'>" . $lastBallInfoArray['UNFACING_BATSMAN_FOURS'] . "</td>
        <td align='center'>" . $lastBallInfoArray['UNFACING_BATSMAN_SIXES'] . "</td>
        </tr>";
        $liveViewDisplay .= "</table>";

        $liveViewDisplay .= "</div>";
        return $liveViewDisplay;
    }

    public function showLiveViewDisplay() {
        return $this->buildLiveViewDisplay();
    }

    public function getWagonWheel() {
        return $this->buildCanvas();
    }

}